package com.library_manage_system.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.library_manage_system.base.Observer;
import com.library_manage_system.base.WindowsMain;
import com.library_manage_system.strategy.BorrowBookPanelStrategy;
import com.library_manage_system.strategy.FindBookPanelStrategy;
import com.library_manage_system.strategy.LoggingPanelStrategy;
import com.library_manage_system.strategy.ModifyBookPanelStrategy;
import com.library_manage_system.strategy.ReturnBookPanelStrategy;
import com.library_manage_system.strategy.StaticsPanelStrategy;
import com.library_manager_system.Book;
import com.library_manager_system.utils.LibraryDao;
import com.library_manager_system.utils.PanelFactory;

public class WindowsMainImpl extends JFrame implements WindowsMain{
	
	private ArrayList<Observer> observers = new ArrayList<Observer>();
	private CardLayout cardLayout;
	private static JPanel cardPanel;
	private static JPanel initBookDataLoggingPanel;

	private static WindowsMainImpl uniqueInstance;
	public static WindowsMainImpl getInstance(){
		if(uniqueInstance== null){
			ArrayList<Book> arrayList = new ArrayList<Book>();
			arrayList = LibraryDao.readBook(1);
			uniqueInstance = new WindowsMainImpl("广财图书馆管理系统",650,500,arrayList);
		}
		return uniqueInstance;
	}
	
	// 构造方法
	public WindowsMainImpl(String title, int width, int heigh,ArrayList<Book> arraylist) {
		// 设置窗口的标题
		super(title);

		// 拿到一个toolkit对象，用于获取屏幕的宽和高
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		double screenWidth = screenSize.getWidth();
		double screenHeigh = screenSize.getHeight();

		int x = (int) (screenWidth - width) / 2;
		int y = (int) (screenHeigh - heigh) / 2;
		setBounds(x, y, width, heigh);
		//setResizable(false);

		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout(3, 3));
		contentPane.setBackground(Color.CYAN);

    	//设置主面板的上部为广东财经大学的图片
		JPanel iconPanel = new JPanel();
		ImageIcon imageicon = new ImageIcon("广财.png");
		JLabel jlabelImage = new JLabel(imageicon);
		iconPanel.add(jlabelImage);
//		iconPanel.setPreferredSize(new Dimension(100,150));
		contentPane.add(iconPanel, BorderLayout.NORTH);

		//添加观察者
		BookDataLoggingPanel b1 = BookDataLoggingPanel.getInstance(this);
		b1.SetPanelStrategy(new LoggingPanelStrategy());
		BorrowBookPanel b2 = BorrowBookPanel.getInstance(this);
		b2.SetPanelStrategy(new BorrowBookPanelStrategy());
		ReturnBookPanel b5 = new ReturnBookPanel(this);
		b5.SetPanelStrategy(new ReturnBookPanelStrategy());
		FindBookPanel b3 = FindBookPanel.getInstance(this);
		b3.SetPanelStrategy(new FindBookPanelStrategy());
		ModifyBookPanel b4 = new ModifyBookPanel(this);
		b4.SetPanelStrategy(new ModifyBookPanelStrategy());
		StatisticsPanel b6 = new StatisticsPanel(this);
		b6.SetPanelStrategy(new StaticsPanelStrategy());
		
		System.out.println("观察者"+observers.size());
		
		// 设置右边的面板为操作选项
		initBookDataLoggingPanel = PanelFactory.createPanel("BookDataLoggingPanel",arraylist,this);
		// 新建一个卡片panel
		cardPanel = new JPanel();
		
		// 设置cardPanel为卡片布局
		cardLayout = new CardLayout(3, 3);
		cardPanel.setLayout(cardLayout);
		

		// 为cardPanel添加卡片图书资料录入的panel
		init(arraylist,this);

		// 把cardPanel部署到窗口上去
		contentPane.add(cardPanel, BorderLayout.CENTER);

		// 设置左边的面板为选择操作面板
		JPanel selectPanel = new SelectPanel().selectPanel(cardLayout,cardPanel,arraylist);
		contentPane.add(selectPanel, BorderLayout.WEST);

		// 设置窗口的一些属性
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
	}
	public static Boolean init(ArrayList<Book> arraylist,WindowsMainImpl windowsMainImpl){
		JPanel borrowBookPanel = PanelFactory.createPanel("BorrowBookPanel", arraylist,windowsMainImpl);
		JPanel returnboookPanel = PanelFactory.createPanel("ReturnBookPanel", arraylist,windowsMainImpl);
		JPanel findBookPanel = PanelFactory.createPanel("FindBookPanel", arraylist,windowsMainImpl);
		JPanel modifyBookPanel = PanelFactory.createPanel("ModifyBookPanel", arraylist,windowsMainImpl);
		JPanel staticsPanel = PanelFactory.createPanel("StatisticsPanel", arraylist,windowsMainImpl);
		
		
		cardPanel.add(initBookDataLoggingPanel, "loggingPanel");
		cardPanel.add(borrowBookPanel,"borrowBookPanel");
		cardPanel.add(returnboookPanel,"returnboookPanel");
		cardPanel.add(findBookPanel,"findBookPanel");
		cardPanel.add(modifyBookPanel,"modifyBookPanel");
		cardPanel.add(staticsPanel,"staticsPanel");
		return true;
	}
	
	
	
	@Override
	public void registerObserver(Observer o) {
		// TODO Auto-generated method stub
		observers.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		// TODO Auto-generated method stub
		int i = observers.indexOf(o);
		if(i>=0){
			observers.remove(i);
		}
	}

	@Override
	public void notifyObserver(ArrayList<Book> changebooklist) {
		// TODO Auto-generated method stub
		ArrayList<JPanel> panellist = new ArrayList<JPanel>();
		System.out.println(observers.size());
		for(int i = 0;i<observers.size();i++){
			Observer observer = (Observer)observers.get(i);
			JPanel jpanel = observer.update(changebooklist,this);
			panellist.add(jpanel);
		}
			
			cardPanel.add(panellist.get(0), "loggingPanel");
			cardPanel.add(panellist.get(1),"borrowBookPanel");
			cardPanel.add(panellist.get(2),"returnboookPanel");
			cardPanel.add(panellist.get(3),"findBookPanel");
			cardPanel.add(panellist.get(4),"modifyBookPanel");
			cardPanel.add(panellist.get(5),"staticsPanel");
	}
	
	public void  bookListChange(ArrayList<Book> changebooklist){
		notifyObserver(changebooklist);
	}
	
}
